# 度小满

import time

from selenium.webdriver import Chrome
from selenium.webdriver.common.by import By
from DB import query
# from DB import delete


# from selenium.webdriver.support.ui import WebDriverWait
# from selenium.webdriver.support import expected_conditions as EC


def login(username, password):
    driver = Chrome()

    driver.get("http://localhost:90")
    # 全屏
    driver.maximize_window()
    time.sleep(1)
    driver.find_element(By.ID, "username").send_keys(username)
    driver.find_element(By.ID, "password").send_keys(password)
    driver.find_element(By.ID, "login_btn").click()
    time.sleep(3)
    a = driver.find_element(By.CLASS_NAME, "navbar-brand")
    if a.text == "个人理财系统":
        print("登录成功")
    elif a.text == "后台管理系统":
        print("登录成功")
        driver.quit()
        return True
    else:
        print("登录失败")
    return driver


def register(username, password):
    driver = Chrome()

    driver.get("http://localhost:90")
    time.sleep(1)
    driver.find_element(By.XPATH, "/html/body/div/div/div/div[2]/a").click()
    time.sleep(1)
    driver.find_element(By.NAME, "username").send_keys(username)
    time.sleep(1)
    driver.find_element(By.NAME, "password").send_keys(password)
    time.sleep(1)
    driver.find_element(By.NAME, "repassword").send_keys(password)
    time.sleep(1)
    driver.find_element(By.ID, "login_btn").click()
    data = query("SELECT * FROM user WHERE username = '{}'".format(username))
    print("data:", data)
    if len(data) == 1:
        print("注册成功")
        # delete("delete from user where username='{}'".format(username))
        driver.quit()
        return True
    else:
        print("注册失败")
        driver.quit()
    # 添加断言
    assert len(data) == 1


def admin_Delete(username, password):
    driver = Chrome()

    driver.get("http://localhost:90")

    driver.maximize_window()
    time.sleep(1)
    driver.find_element(By.ID, "username").send_keys(username)
    driver.find_element(By.ID, "password").send_keys(password)
    driver.find_element(By.ID, "login_btn").click()
    time.sleep(3)
    a = driver.find_element(By.CLASS_NAME, "navbar-brand")
    if a.text == "个人理财系统":
        print("登录成功")
    elif a.text == "后台管理系统":
        print("登录成功")
    else:
        print("登录失败")
        return driver

    driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/a').click()
    time.sleep(1)
    driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/ul/li[1]/a').click()
    time.sleep(1)
    driver.find_element(By.CLASS_NAME, 'btn.btn-danger.btn-sm.delete_btn')
    if len(driver.find_elements(By.CLASS_NAME, 'btn.btn-danger.btn-sm.delete_btn')) > 1:
        driver.find_elements(By.CLASS_NAME, 'btn.btn-danger.btn-sm.delete_btn')[1].click()
    else:
        print("删除失败")
        driver.quit()
        return False
    time.sleep(3)
    a = driver.find_element(By.XPATH, "/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]")
    print(a)
    a.click()
    time.sleep(3)
    print("删除成功")
    return True


def admin_add_user(username, password, userInfo):
    driver = Chrome()

    driver.get("http://localhost:90")
    driver.maximize_window()
    time.sleep(1)
    driver.find_element(By.ID, "username").send_keys(username)
    driver.find_element(By.ID, "password").send_keys(password)
    driver.find_element(By.ID, "login_btn").click()
    time.sleep(3)
    a = driver.find_element(By.CLASS_NAME, "navbar-brand")
    if a.text == "个人理财系统":
        print("登录成功")
    elif a.text == "后台管理系统":
        print("登录成功")
    else:
        print("登录失败")

    driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/a').click()
    time.sleep(1)
    driver.find_element(By.XPATH, '//*[@id="leftbaraside"]/div[2]/nav/ul/li[2]/ul/li[1]/a').click()
    time.sleep(1)
    driver.find_element(By.XPATH, '//*[@id="user_add_modal_btn"]').click()
    time.sleep(1)
    driver.find_element(By.NAME, "username").send_keys(userInfo['username'])
    time.sleep(1)
    driver.find_element(By.NAME, "password").send_keys(userInfo['password'])
    time.sleep(1)
    driver.find_element(By.NAME, "realname").send_keys(userInfo['realname'])
    time.sleep(1)
    driver.find_element(By.NAME, "phone").send_keys(userInfo['phone'])
    time.sleep(1)
    driver.find_element(By.NAME, "email").send_keys(userInfo['email'])
    time.sleep(1)
    a = driver.find_element(By.XPATH, '//*[@id="user_save_btn"]')
    # 查询
    print("a:", a)
    try:
        a.click()
    except:
        print("点击失败")

    data = query("SELECT * FROM user WHERE username = '{}'".format(userInfo['username']))
    print("data:", data)
    if len(data) == 1:
        print("注册成功")
        # delete("delete from user where username='{}'".format(username))
        driver.quit()
        return True
    else:
        print("注册失败")
        driver.quit()
        return False


if __name__ == '__main__':
    userName = '2969476744'
    passWord = '123456'
    admin_userName = 'admin'
    admin_password = '123456'
    UserInfo = {
        'username': '1228912646',
        'password': '123456',
        'realname': '朱弘光',
        'phone': '15081349872',
        'email': '1228912646@qq.com'
    }

    # register(userName, passWord)
    # login(userName, passWord)
    # login(admin_userName, admin_password)
    # admin_Delete(admin_userName, admin_password)
    # admin_add_user(admin_userName, admin_password, UserInfo)
